package Controller.GoodsCategory;


import Dto.GoodsCategory;
import Service.GoodsCategoryService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;
import java.time.LocalDateTime;

@WebServlet(name = "addGoodsCategory", value = "/addGoodsCategory")
public class AddGoodsCategory extends HttpServlet {
    GoodsCategoryService goodsCategoryService = new GoodsCategoryService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String name = request.getParameter("name");
        String describe = request.getParameter("describe");
        LocalDateTime nowDate = LocalDateTime.now();
        GoodsCategory goodsCategory = new GoodsCategory();
        goodsCategory.setName(name);
        goodsCategory.setDescribe(describe);
        goodsCategory.setCreatetime(nowDate.toString());
        try{
            goodsCategoryService.addCategory(goodsCategory);
            request.getRequestDispatcher("/ListGoodsCategory").forward(request,response);
        }catch (SQLException e){
            e.printStackTrace();
        }
    }
}